using UnityEngine;
using QFramework;
using Unity.Burst.Intrinsics;
using Unity.VisualScripting;
using QAssetBundle;

namespace ProjectSurvivor
{
	public partial class EnemyMinBoss : ViewController,IEnemy//表示一个挂在场景物体上的控制器（如玩家、刷怪器）。
	{
		public Color DissolveColor;
		public enum States
		{
			FlowingPlayer,
			Warning,//警戒状态
			Desh,//冲向主角
			Wait//等待
		}
		public float speed = 1;
		public float HP = 50;
		public FSM<States> FSM = new FSM<States>();
		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
			FSM.State( States.FlowingPlayer).OnFixedUpdate(()=>
			{
				if (Player.Default)
				{
					var direction = (Player.Default.transform.position - transform.position).normalized;
					SelfRigidbody2D.velocity = direction * speed;
					if ((Player.Default.transform.Position()-transform.Position()).magnitude<=20)
					{
						FSM.ChangeState(States.Warning);
					}
				}
				else
				{
					SelfRigidbody2D.velocity = Vector3.zero;
				}
				
			});
			
			FSM.State(States.Warning)
		.OnEnter(() =>
			{
				SelfRigidbody2D.velocity = Vector2.zero;
			}).OnUpdate(() =>
			{
				if (Player.Default)
				{
					//21~3
					var frames = 3 + (60 * 3 - FSM.FrameCountOfCurrentState) / 10;
					if (FSM.FrameCountOfCurrentState / frames % 2 == 0)
					{
						Triangle.color = Color.red;
					}
					else
					{
						Triangle.color = Color.white;
					}

					if (FSM.FrameCountOfCurrentState >= 60 * 3)
					{
						FSM.ChangeState(States.Desh);
					}
				}
			})
			.OnExit(()=>
			{
				Triangle.color = Color.white;
			});

	
			var dashStartPos = Vector3.zero;
			var dashStartDistanceToPlayer = 0f;
			FSM.State( States.Desh).OnEnter(()=>
			{
				var dicrection = (Player.Default.Position() - transform.Position()).normalized;
				SelfRigidbody2D.velocity = dicrection * 30;
				dashStartPos = transform.Position();
				dashStartDistanceToPlayer=(Player.Default.Position() - transform.Position()).magnitude;
			}).OnUpdate(()=>
			{
				if (Player.Default)
				{
					var distance = (transform.Position() - dashStartPos).magnitude;
					if (distance >= dashStartDistanceToPlayer + 5)
					{
						FSM.ChangeState(States.FlowingPlayer);
					}
				}
			});
			FSM.StartState( States.FlowingPlayer);
		}
        void OnDestroy()
        {
            EnemyGenerator.EnemyCount.Value--;
        }
		void FixedUpdate()
		{
			FSM.FixedUpdate();
        }
        void Update()
		{
			if (FSM != null)
			{
				FSM.Update();
			}
			
			
			if (HP <= 0)
			{
				Global.GeneratePowerUp(gameObject,true);
				FxController.Play(Triangle,DissolveColor);
				AudioKit.PlaySound(Sfx.ENEMY_DIE);
				this.DestroyGameObjGracefully();
			}
        }

		bool mignoreHurt = false;
        public void Hurt(float value,bool force=false)
		{
			if (mignoreHurt&&!force) return;
			FloattingTextController.Play(transform.position+Vector3.up*0.5f,value.ToString("0"));
			Triangle.color = Color.red;
			//下一帧NextFrame
			AudioKit.PlaySound("enemy_die");
			ActionKit.Delay(0.2f, () =>//注册帧更新/UI调试回调；
			{				
				HP -= value;
				Triangle.color = Color.black;
				mignoreHurt = false;
			}).Start(this);

		}

         public void SetHpScale(float hpScale)
        {
			HP *= hpScale;
        }

        public void SetSpeesScale(float speedScale)
        {
            speed *= speedScale;
        }

        public void Hurt(float value, bool force = false, bool critic = false)
        {
            throw new System.NotImplementedException();
        }

        public void SetHPScale(float speedScale)
        {
           
        }
    }
}
